﻿
@{
    ViewData["Title"] = "UpdateView";
    Layout = null;
}




<link href="~/libs/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />

<form class="layui-form" action="">
    <div class="layui-form-item">
        <label class="layui-form-label">角色</label>
        <div class="layui-input-block">
            <input type="text" id="roleName" name="roleName" autocomplete="off" placeholder="请输入角色" lay-verify="required" class="layui-input">
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">是否启用</label>
        <div class="layui-input-block">
            <input type="radio" id="roleState" name="roleState" value="1" title="是" checked>
            <input type="radio" id="roleState" name="roleState" value="0" title="否">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary" onclick="closeWorkstation()">返回</button>
        </div>
    </div>
</form>

<script src="~/libs/layui-v2.5.6/layui/layui.js"></script>
<script src="~/libs/jquery/jquery.js"></script>


<script>
    // 页面加载完成后执行的函数
    $(function () {
        mainUnitListOne()
    });

    //获取主数据进行反填
    function mainUnitListOne() {
        $.ajax({
            url: '/Role/Backfill',
            type: 'get',
            data: {
                id: @ViewBag.Id
            },
            dataType: 'json',
            success: function (res) {
                $("#roleName").val(res.roleName);
                $("#roleState").val(res.roleState);;
                layui.form.render("radio");
            }
        });
    }

    layui.use(['form', 'laydate', 'util'], function () {
    var form = layui.form;
    var layer = layui.layer;
    var laydate = layui.laydate;
    var util = layui.util;

      // 提交事件
      form.on('submit(demo1)', function(data){
          console.log(data);
          var field = data.field; // 获取表单字段值
          field.id= @ViewBag.Id;
          field.roleState=parseInt(field.roleState)

          $.ajax({
              url:'/Role/Update',
              type:'post',
              data:field,
              dataType:'json',
              success:function(res){
                  console.log(res);
                  if(res>0){
                       layer.msg('修改成功');
                       setTimeout(() => {
                           closeWorkstation();
                       }, 2000);
                  }else{
                       layer.msg('修改失败角色名重复');
                       return
                  }
              }
          })
          return false; // 阻止默认 form 跳转
      });


    });


    //关闭弹框
    function closeWorkstation() {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前的索引
        parent.layer.close(index); //再执行关闭
    }
</script>


